﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Police_SoldierArchive_Allocation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            BindData(0);
            bindDepartmentId();
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindData(PageIndex);
    }
    void bindDepartmentId()
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4) + "000";
        string where = " departmentId like '" + departmentId.Substring(0, 4) + "%' ";
        ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList(where);
        ddlDepartment.DataBind();
    }
    private void BindData(int Pageindex)
    {
        string departmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4) + "000";
        string where = " departmentid ='" + departmentId + "' ";
        NewSoldier ns = new NewSoldier();
        ListPageContral1.ItemCount = ns.GetSoldiersCountByWhere(where);
        gvSoldiers.DataSource = ns.GetSoldiersByWhere(where, "setTime", "desc", 20, Pageindex);
        gvSoldiers.DataBind();
    }

    protected void gvSoldiers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (ListPageContral1.CurrentPageIndex * 20 + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            SoldierArchiveInfo soldier = e.Row.DataItem as SoldierArchiveInfo;
            System.Web.UI.HtmlControls.HtmlAnchor AName = e.Row.FindControl("AName") as System.Web.UI.HtmlControls.HtmlAnchor;

            AName.InnerText = soldier.SoldierName;

            if (soldier.IsContract)
            {
                //AName.HRef = "javascript:OpenWin('EmployeeView.aspx?ID=" + soldier.SoldierID + "')";

                AName.HRef = "EmployeeView.aspx?ID=" + soldier.SoldierID;

            }
            else
            {
                //AName.HRef = "javascript:OpenWin('SoldierView1.aspx?ID=" + soldier.SoldierID + "')";
                AName.HRef = "SoldierView1.aspx?ID=" + soldier.SoldierID;

            }

        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            NewSoldier soldier = new NewSoldier();
            foreach (GridViewRow row in gvSoldiers.Rows)
            {
                CheckBox CheckItem = row.FindControl("CheckItem") as CheckBox;
                if (CheckItem.Checked)
                {
                    string SoldierID = gvSoldiers.DataKeys[row.RowIndex].Values[0].ToString();
                    string Departmentid = ddlDepartment.SelectedValue;
                    soldier.UpdateSodiersToFireBase(new Guid(SoldierID), Departmentid);
                }
            }
            Prompt.Text = "提示：分配兵员成功！";
        }
        catch (Exception ex)
        {
            Prompt.Text = ex.Message;
        }
        ListPageContral1.SetFirstPage();
        BindData(0);
    }
}
